//
//  SecondViewController.m
//  UIhomework_14
//
//  Created by dllo on 15/12/31.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "SecondViewController.h"
#import "MovieModel.h"
#import "FilemTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "SecondViewController2.h"
#import "SecondViewController3.h"
@interface SecondViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *tableview;
@property (nonatomic, retain) NSMutableArray *arr;
@property (nonatomic, retain) NSMutableArray *arrModel;
@property (nonatomic, retain) UIBarButtonItem *item;
@end

@implementation SecondViewController

-(void)dealloc{
    [_arr release];
    [_tableview release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.barTintColor = [UIColor cyanColor];
    self.navigationItem.title = @"电影";
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_nav_collection@2x.png"] style:UIBarButtonItemStylePlain target:self action:@selector(itemAction:)];
    self.navigationItem.rightBarButtonItem = item;
    [item release];
    
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    [self.view addSubview:_tableview];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    [_tableview registerClass:[FilemTableViewCell class] forCellReuseIdentifier:@"cell"];
    [_tableview release];
    [self dataHandel];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//数据处理
- (void)dataHandel{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"movie" ofType:@"plist"];
    self.arr = [NSMutableArray arrayWithContentsOfFile:path];
    //NSLog(@"%@", _arr);
    self.arrModel = [NSMutableArray array];
    for (NSDictionary *dic in _arr) {
        MovieModel *model = [[MovieModel alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        [_arrModel addObject:model];
    }
    //NSLog(@"%@", _arrModel);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _arrModel.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 250;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    SecondViewController2 *secVC = [[SecondViewController2 alloc] init];
    secVC.index = indexPath;
    secVC.modelArr = _arrModel;
    [self.navigationController pushViewController:secVC animated:YES];
    [secVC release];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    FilemTableViewCell *cell = [[FilemTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
    cell.imgview.image = [UIImage imageNamed:@"bg_eventlistcell.png"];
    MovieModel *model = _arrModel[indexPath.row];
    [cell.imgview1 sd_setImageWithURL:[NSURL URLWithString:model.image]];
    cell.label.text = model.title;
    return cell;
}

- (void)itemAction:(UIBarButtonItem *)item{
    SecondViewController3 *secVC = [[SecondViewController3 alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:secVC];
    [self presentViewController:nav animated:YES completion:^{
        
    }];
    [secVC release];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
